// ================客户采购目录管理(cust_procurement_manager)


/**
 * 提交自定义业务服务
 * map:{formListId : "", // select列表id
 * 	reloadListId : "",// 重新加载的list
 * 	serviceName : "",// 服务名称
 * 	msg :"", // 提示内容
 * 	closeModalId:"", // 需要关闭的对话框id, 多个用","隔开
 * 	param : {} // 提交参数 
 * }
 */
function toCallService(map) {

    var selectKeys = h3ui.widgets[map.formListId].props.selectedRowKeys;
    let length = selectKeys.length;
    if (length == 0) {
        h3ui.$message.info("请选择一行数据");
        return false;
    }
    map.param.ids = selectKeys;

    h3ui.$confirm({
        title: map.msg + '确认提交吗',
        onOk() {
            h3ui.services[map.serviceName](map.param).then(res => {
                if (res.success) {
                    h3ui.$message.success('提交成功');

                    if (map.reloadListId) {
                        reloadList(map.reloadListId);
                    }

                    if (map.closeModalId) {
                        map.closeModalId.split(",").forEach(a => {
                            if (a) h3ui.widgets[a].props.visible = false;
                        })
                    }

                } else {
                    h3ui.$message.error(res.errMessage || '提交失败');
                }
            })
        }
    });
}

//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//标签页切换 刷新列表, 要求 标签页id 是 "xxx"+"_"+listid
function tabLoadList(evt) {
    doReloadList({node: {id: evt.data.activeKey.split("_")[1]}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];


    var filter = h3ui.widgets[list.childIds[0]].props.value;

    var pageIndex = evt.pageIndex;
    var pageSize = 10;
    if (!pageIndex) {
        pageIndex = h3ui.widgets[list.childIds[3]].props.current;
    }

    list.emit("load", {pageIndex, pageSize, filter});

    list.props.selectedRowKeys = [];
}


//客户商品 上下架
function purchaseSaled(evt) {

    var selectListId = evt.node.id.split("_")[1];
    var reloadListId = selectListId;

    toCallService({
        formListId: selectListId,
        reloadListId: reloadListId,
        serviceName: "purchaseSaled",
        msg: "上下架",
        closeModalId: "",
        param: {},
    });
}


// 客户商品 启禁用
function purchaseEnabled(evt) {

    var selectListId = evt.node.id.split("_")[1];
    var reloadListId = selectListId;

    toCallService({
        formListId: selectListId,
        reloadListId: reloadListId,
        serviceName: "purchaseEnabled",
        msg: "启禁用",
        closeModalId: "",
        param: {},
    });
}


//打开供应商更换弹框
function openChangeVendor() {
    console.log(123123);
    var rows = h3ui.widgets.FormList3.props.selectedRow;

    if (rows.length != 1) {
        h3ui.$message.info("请选择一行数据");
        return;
    }
    var row = rows[0];

    if (row.if_approval == "0") {
        h3ui.$message.info("当前数据在审批中,无法更换供应商");
        return;
    }


    h3ui.models.t_goods_purchase.newItem();
    var editFiled = ["reason", "vendor_id"];

    h3ui.widgets.FormModel1.childIds.map(a => h3ui.widgets[a].props.code)
        .filter(a => a).forEach(a => h3ui.models.t_goods_purchase.item[a] = row[a]);

    //把当前供应商赋值给old_vendor_id
    h3ui.models.t_goods_purchase.item["old_vendor_id"] = row.vendor_id;
    h3ui.models.t_goods_purchase.item["reason"] = null;
    h3ui.models.t_goods_purchase.item["id"] = row.id;

    h3ui.widgets.FormModel1.childIds.forEach(a => {
        var field = h3ui.widgets[a].props;
        if (editFiled.includes(field.code)) {
            h3ui.widgets[a].props.editable = true;
        } else {
            h3ui.widgets[a].props.editable = false;
        }

    });

    toGetProgressOrder(row.id);

    h3ui.widgets.CustomModal1.props.visible = true;

}

// 列表数据加载
function toGetProgressOrder(id) {
    h3ui.services.toGetProgressOrder({id}).then(res => {
        if (res.success) {
            h3ui.widgets.Table1.props.dataSource = res.data;
        } else {
            h3ui.widgets.Table1.props.dataSource = {
                "total": 10,
                "items": [{"id": "", "pretaxAmount": "", "amount": "", "receiver": "", "phone": ""}]
            };
        }
    });
}

// 发起供应商审批
function toSummitChangeVendor() {


    var dataSource = h3ui.models.t_goods_purchase;
    var {id, vendor_id, reason, old_vendor_id} = dataSource.item;

    if (vendor_id == null || vendor_id.length == 0) {
        h3ui.$message.info("请选择供应商");
        return;
    }

    if (old_vendor_id != null && vendor_id.length == 1
        && old_vendor_id[0].id == vendor_id[0].id) {

        h3ui.$message.info("新供应商不能与原供应商相同");
        return;
    }


    var data = {id, vendor_id, reason};
    //执行convert 前深拷贝下对象
    data = JSON.parse(JSON.stringify(data));
    dataSource.convert(data, dataSource.schema);

    var {id, vendor_id, reason} = data;
    var param = {id, vendor_id, reason};

    console.log(param);

    h3ui.services.changeVendorSubmmit(param).then(res => {

        if (res.success) {
            h3ui.$message.success('提交成功');

            h3ui.widgets.CustomModal1.props.visible = false;

            doReloadList({node: {id: "FormList3"}});
        } else {
            h3ui.$message.error(res.errMessage || '提交失败');
        }
    })


}


//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];


    var filter = h3ui.widgets[list.childIds[0]].props.value;

    var pageIndex = evt.pageIndex;
    var pageSize = 10;
    if (!pageIndex) {
        pageIndex = h3ui.widgets[list.childIds[3]].props.current;
    }

    list.emit("load", {pageIndex, pageSize, filter});

    list.props.selectedRowKeys = [];
}


//打开供应商审批
function openApprovalVendor(evt) {

    var modalId = "CustomModal2";


    var rows = h3ui.widgets.FormList3.props.selectedRow;

    if (rows.length != 1) {
        h3ui.$message.info("请选择一行数据");
        return;
    }
    var row = rows[0];

    if (row.if_approval != 0) {
        h3ui.$message.info("暂无需要审批的任务~");
        return;
    }

    var user = JSON.parse(localStorage.getItem("user"));

    if (row.task_id != user.userInfo.id) {
        h3ui.$message.info("审批人不匹配");
        return;
    }

    //绑定数据id
    h3ui.widgets.CustomModal2.props.rowId = row.id;

    //初始化数据
    h3ui.widgets.RadioGroup1.props.value = null;
    h3ui.widgets.Textarea4.props.value = null;
    h3ui.widgets[modalId].props.visible = true;


}

//提交供应商审批
function submitApprovalVendor(evt) {

    var modalId = "CustomModal2";
    var id = h3ui.widgets.CustomModal2.props.rowId;
    var approval = h3ui.widgets.RadioGroup1.props.value;
    var reason = h3ui.widgets.Textarea4.props.value;

    if (approval == null) {
        h3ui.$message.info("审批结果需要填写");
        return;
    }

    h3ui.services.changeVendorApproval({approval, reason, id}).then(res => {
        if (res.success) {
            h3ui.$message.success('提交成功');
            h3ui.widgets[modalId].props.visible = false;
            doReloadList({node: {id: "FormList3"}});
        } else {
            h3ui.$message.error(res.errMessage || '提交失败');
        }
    })


}






